<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        form {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        h2 {
            text-align: center;
            color: #333;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 8px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }

        input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        input[type="submit"]:hover {
            background-color: #0056b3;
        }
    </style>
</head>

<body>
    <form action="" method="post">
        <h2>注册</h2>
        <label for="username">用户名:</label>
        <input type="text" id="username" name="username" required>
        <label for="password">密码:</label>
        <input type="password" id="password" name="password" required>
        <input type="submit" name="register" value="注册">
        <br>
        已有帐号?<a href="login.php">返回登录</a>
    </form>
    <?php
    if (isset($_POST['register'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];

        $url = 'http://localhost:8081/user/register';
        $data = array('uname' => $username, 'password' => $password);
        $data_json = json_encode($data);

        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => "Content-type: application/json\r\n" .
                    "Content-Length: " . strlen($data_json) . "\r\n",
                'content' => $data_json
            )
        );

        $context = stream_context_create($options);
        $result = @file_get_contents($url, false, $context);

        if ($result === false) {
            $error = error_get_last();
            echo "<p style='color: red;'>请求失败: ". $error['message']. "</p>";
        } else {
            $response = json_decode($result, true);
            if ($response && $response['code'] == "0") {
                echo "<p style='color: green;'>注册成功！欢迎，". $response['data']['uname']. "</p>";
            } else {
                echo "<p style='color: red;'>注册失败: ". $response['msg']. "</p>";
            }
        }
    }
    ?>
</body>

</html>